﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using DAL;
using System.Data.SqlClient;
using Wuqi.Webdiyer;

namespace BLL
{
    public class PageManage
    {
        private PageManage() { }
        public static void doPaging(string table, string query, AspNetPager webPage, string where, string key, string sort, Repeater rp, int showPage)
        {
            if (query == "")
                query = "*";
            if (sort == "")
            {
                sort = "ID desc";//asc
            }
            if (sort == "asc")
            {
                sort = "ID asc";
            }
            var anyWhere = " where  ";
            if (where != "")
            {
                where = " where " + where;
                anyWhere = where + " and  ";
            }
            var notIn = (showPage - 1) * webPage.PageSize;
            string cmdText = "";
            var ds = new DataSet();
            if (notIn == 0)
                cmdText = String.Format("select top {0} {1} from [{2}] {3} order by {4}", webPage.PageSize, query, table, where, sort);
            else
                cmdText = String.Format("select top {0} {1} from [{2}] {3} {4} not in (select top {6} {4} from [{2}] {7} order by {5} )order by {5}", webPage.PageSize, query, table, anyWhere, key, sort, notIn, where);
            ds = SqlHelper.ExecuteDataset(cmdText, null);
            var countSql = String.Format("select count(*) from [{0}] {1}", table, where);
            webPage.RecordCount = Convert.ToInt32(SqlHelper.ExecuteScalar(countSql, null));
            rp.DataSource = ds;
            rp.DataBind();
        }
        public static void doPaging(string table, string query, AspNetPager webPage, string where, string key, string sort, Repeater rp)
        {
            if (query == "")
                query = "*";
            if (sort == "")
            {
                sort = "ID desc";//asc
            }
            if (sort == "asc")
            {
                sort = "ID asc";
            }
            var anyWhere = " where ";
            if (where != "")
            {
                where = " where " + where;
                anyWhere = where + " and ";
            }
            if (key == "")
            {
                key = "ID";
            }
            var notIn = (webPage.CurrentPageIndex - 1) * webPage.PageSize;
            string cmdText = "";
            var ds = new DataSet();
            if (notIn == 0)
                cmdText = String.Format("select top {0} {1} from [{2}] {3} order by {4}", webPage.PageSize, query, table, where, sort);
            else
                cmdText = String.Format("select top {0} {1} from [{2}] {3} {4} not in (select top {6} {4} from [{2}] {7} order by {5} )order by {5}", webPage.PageSize, query, table, anyWhere, key, sort, notIn, where);
            ds = SqlHelper.ExecuteDataset(cmdText, null);
            var countSql = String.Format("select count(*) from [{0}] {1}", table, where);
            webPage.RecordCount = Convert.ToInt32(SqlHelper.ExecuteScalar(countSql, null));
            rp.DataSource = ds;
            rp.DataBind();
        }

        ///// <summary>
        ///// OLE数据库
        ///// </summary>
        ///// <param name="table"></param>
        ///// <param name="query"></param>
        ///// <param name="webPage"></param>
        ///// <param name="where"></param>
        ///// <param name="key"></param>
        ///// <param name="sort"></param>
        ///// <param name="rp"></param>
        ///// <param name="isOLE"></param>
        //public static void doPaging(string table, string query, AspNetPager webPage, string where, string key, string sort, Repeater rp)
        //{
        //    if (query == "")
        //        query = "*";
        //    if (sort == "")
        //        sort = "desc";
        //    var anyWhere = " where ";
        //    if (where != "")
        //    {
        //        where = " where " + where;
        //        anyWhere = where + " and ";
        //    }
        //    var notIn = (webPage.CurrentPageIndex - 1) * webPage.PageSize;
        //    string cmdText = "";
        //    var ds = new DataSet();
        //    if (notIn == 0)
        //        cmdText = String.Format("select top {0} {1} from [{2}] {3} order by {4} {5}", webPage.PageSize, query, table, where, key, sort);
        //    else
        //        cmdText = String.Format("select top {0} {1} from [{2}] {3} {4} not in (select top {6} {4} from [{2}] {7} order by {4} {5})order by {4} {5}", webPage.PageSize, query, table, anyWhere, key, sort, notIn, where);
        //    ds = OleHelper.ExecuteDataset(cmdText, null);
        //    var countSql = String.Format("select count(*) from [{0}] {1}", table, where);
        //    webPage.RecordCount = Convert.ToInt32(OleHelper.ExecuteScalar(countSql, null));
        //    rp.DataSource = ds;
        //    rp.DataBind();
        //}
        public static void doPaging(string table, AspNetPager webPage, Repeater rp)
        {
            var a = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "proc_doPaging", new SqlParameter("@Table", table), new SqlParameter("@PageSize", webPage.PageSize), new SqlParameter("@ShowPage", webPage.CurrentPageIndex));
            webPage.RecordCount = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "proc_GetAllPage", new SqlParameter("@Table", table));
            rp.DataSource = a;
            rp.DataBind();
        }
        public static void doPaging(string table, string key, AspNetPager webPage, Repeater rp)
        {
            var a = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "proc_doPaging", new SqlParameter("@Table", table), new SqlParameter("@Key", key), new SqlParameter("@PageSize", webPage.PageSize), new SqlParameter("@ShowPage", webPage.CurrentPageIndex));
            webPage.RecordCount = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "proc_GetAllPage", new SqlParameter("@Table", table));
            rp.DataSource = a;
            rp.DataBind();
        }
    }
}
